package com.zoke.daycarpool.thirdapi;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.UMAuthListener;
import com.umeng.socialize.controller.listener.SocializeListeners.UMDataListener;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.zoke.daycarpool.config.CarpoolConfig;
import com.zoke.daycarpool.config.CarpoolConfig.OpenPlatform;

/**
 * @author JackWu
 */
public class QQ {
	/****************
	 * 发起添加群流程。群号：Android编程&amp;移动互联(215621863) 的 key 为：
	 * 9gqhSj4gnhFHOpJ2N89zKLRokMgLXD7c 调用
	 * joinQQGroup(9gqhSj4gnhFHOpJ2N89zKLRokMgLXD7c) 即可发起手Q客户端申请加群
	 * Android编程&amp;移动互联(215621863)
	 *
	 * @param key
	 *            由官网生成的key
	 * @return 返回true表示呼起手Q成功，返回fals表示呼起失败
	 ******************/
	public static boolean joinQQGroup(Context context, String key) {
		Intent intent = new Intent();
		intent.setData(Uri
				.parse("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26k%3D"
						+ key));
		// 此Flag可根据具体产品需要自定义，如设置，则在加群界面按返回，返回手Q主界面，不设置，按返回会返回到呼起产品界面
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		try {
			context.startActivity(intent);
			return true;
		} catch (Exception e) {
			// 未安装手Q或安装的版本不支持
			return false;
		}
	}

	/** 第三方登陆 **/
	public static void login(Activity activity, UMAuthListener listener) {
		UMSocialService mController = UMServiceFactory
				.getUMSocialService("com.umeng.login");
		UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(activity,
				OpenPlatform.QQ_APPID, OpenPlatform.QQ_APPKEY); // 添加QQ支持sso
		qqSsoHandler.setTargetUrl(CarpoolConfig.TESTAPP_DOWNLOADURL);
		qqSsoHandler.addToSocialSDK();
		mController.doOauthVerify(activity, SHARE_MEDIA.QQ, listener);
	}

	/** qq登陆完成以后 获取Token值 **/
	public static void getQQToken(Activity activity, UMDataListener listener) {
		// 获取相关授权信息
		UMSocialService mController = UMServiceFactory
				.getUMSocialService("com.umeng.login");
		mController.getPlatformInfo(activity, SHARE_MEDIA.QQ, listener);
	}

}
